package com.gmlive.common.sample.dialog

import android.content.DialogInterface
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.gmlive.common.sample.R
import com.gmlive.common.ui.dialog.IkAlertDialog
import com.gmlive.common.ui.dialog.IkBottomSheetDialog
import com.gmlive.common.ui.dialog.IkLoadingDialog
import kotlinx.android.synthetic.main.activity_dialog_demo.*

class DialogDemoActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_dialog_demo)

        setSupportActionBar(toolbar)

        button1.setOnClickListener {
            showAlert(title = "温馨提示", message = "亲爱的音泡用户\n为了更好地保护未成年人用户",
                    negative = "暂不认证", positive = "立即认证")
        }

        button2.setOnClickListener {
            showAlert(title = "温馨提示", message = "亲爱的音泡用户",
                    link = "了解《音泡未成年人保护计划》", negative = "暂不认证", positive = "立即认证", colorStyle = R.style.IkAlertDialogThemeVins)
        }

        button3.setOnClickListener {
            showAlert(title = "温馨提示", message = "亲爱的音泡用户，为了更好地保护未成年人用户，即日起我们将启动对所有音泡用户的实名认证；请您完成实名认证已便继续使用音泡~",
                    link = "了解《音泡未成年人保护计划》", negative = "知道了")
        }

        button4.setOnClickListener {
            showAlert(message = "亲爱的音泡用户，为了更好地保护未成年人用户，即日起我们将启动对所有音泡用户的实名认证；请您完成实名认证已便继续使用音泡~", positive = "立即认证")
        }

        button5.setOnClickListener {
            showAlert(title = "温馨提示", message = "亲爱的音泡用户，为了更好地保护未成年人用户，即日起我们将启动对所有音泡用户的实名认证；请您完成实名认证已便继续使用音泡~")
        }

        button6.setOnClickListener {
            showBottomSheetDialog()
        }

        button7.setOnClickListener {
            showBottomMenu()
        }

        button8.setOnClickListener {
            showTitleBottomMenu()
        }

        button9.setOnClickListener {
            showBottomMenu(true)
        }

        button10.setOnClickListener {
            showLoadingDialog()
        }

        button11.setOnClickListener {
            showAlert(title = "强制弹框", message = "该弹框适用于一些强制性的业务逻辑，比如强制更新等",
                    negative = "不更新", negativeDismiss = false, positive = "确定", cancelable = false)
        }
    }

    private fun showAlert(title: String? = null,
                          message: String,
                          link: String? = null,
                          linkDismiss: Boolean = true,
                          negative: String? = null,
                          negativeDismiss: Boolean = true,
                          positive: String? = null,
                          positiveDismiss: Boolean = true,
                          cancelable: Boolean = true,
                          useFullWidth: Boolean = false,
                          colorStyle: Int = R.style.IkAlertDialogTheme) {
        val builder = IkAlertDialog.Builder(this, colorStyle)
                .setCancelable(cancelable)
                .setTitle(title)
                .setMessage(message, Gravity.CENTER, useFullWidth)
        link?.let {
            builder.setLink(it, linkDismiss) { dialog, _ ->
                dialog.dismiss()
            }
        }

        negative?.let {
            builder.setNegativeButton(it, negativeDismiss) { _: DialogInterface, _: Int ->
            }
        }

        positive?.let {
            builder.setPositiveButton(it, positiveDismiss) { _: DialogInterface, _: Int ->
            }
        }

        builder.show()
    }

    // bottom sheet dialog 只提供了底部出来的动画效果，显示内容需自定义
    private fun showBottomSheetDialog() {
        val builder = IkBottomSheetDialog.Builder(this)
        val viewFactory = IkBottomSheetDialog.ViewFactory {
            val view = layoutInflater.inflate(R.layout.dialog_bottom_sheet_demo, null)
            view
        }
        builder.setViewFactory(viewFactory)
        builder.show()
    }

    private fun showBottomMenu(isWithColor: Boolean = false) {
        val builder = IkBottomSheetDialog.MenuBuilder(this)
        val menuItems = listOf("从手机相册选择", "拍照")
        val menuItemsWithParam = listOf(IkBottomSheetDialog.ItemBean("关注"), IkBottomSheetDialog.ItemBean("拉黑"), IkBottomSheetDialog.ItemBean("举报", Color.RED))

        if (isWithColor)
            builder.setItemsWithParam(menuItemsWithParam) { dialog, which ->
                dialog.dismiss()
                Toast.makeText(this, "点击了：" + menuItems[which], Toast.LENGTH_SHORT).show()
            }
        else
            builder.setItems(menuItems) { dialog, which ->
                dialog.dismiss()
                Toast.makeText(this, "点击了：" + menuItems[which], Toast.LENGTH_SHORT).show()
            }

        builder.setNegativeButton("取消") { _, _ ->
        }

        builder.show()
    }

    private fun showTitleBottomMenu() {
        val builder = IkBottomSheetDialog.MenuTitleBuilder(this)
        val menuItems = listOf("从手机相册选择", "拍照")

        builder.setMessage("选择上传类型")

        builder.setItems(menuItems) { dialog, which ->
            dialog.dismiss()
            Toast.makeText(this, "点击了：" + menuItems[which], Toast.LENGTH_SHORT).show()
        }

        builder.setNegativeButton("取消") { _, _ ->
        }

        builder.show()
    }

    private fun showLoadingDialog() {
        IkLoadingDialog.Builder(this).setCancelable(true).show()
    }
}
